use async_timer;
use std::time::{Duration};
use futures::executor::block_on;
use async_std::task;
use futures::select;
use futures::FutureExt;

#[test]
fn test_async_timed(){
    println!("start");
    // timed的作用是future执行超时后强行停止future内部的执行
    let _ = block_on(async_timer::timed(async {
        for i in 1..11 {
            println!("job run.{}", i);
            task::sleep(Duration::from_secs(1)).await
        }
    }, Duration::from_secs(3)));

    println!("end");

    block_on(async {
        task::sleep(Duration::from_secs(20)).await
    })

}

#[test]
fn test_async_interval(){
    // 间隔计时器
    let mut interval = async_timer::interval(Duration::from_secs(1));
    block_on(async {
        loop {
            select! {
                () = interval.fuse() => {
                    println!("interval tick.");
                }
            }
        }
    })
}
